<?php
$cs=Yii::app()->getClientScript();
$cs->registerCoreScript('jquery');
$cs->registerScriptFile(SystemAsset::toUrl('jquery.validate.js'));
Yii::app()->getClientScript()->registerScript('callRefreshVerifyImage', 'refreshVerifyImage();');
?>

<script type="text/javascript">
jQuery.validator.addMethod("exclude", function(value, element, param) {
    if(this.optional(element))
        return true;

    var s='';
    for(var i=0; i<param.length; ++i) {
        s+=param[i];
        if(i!=param.length-1) {
            s+='|';
        }
    }
    var pattern = new RegExp(s);
    return !pattern.test(value);
}, jQuery.format("不能包含非法字符"));

$(document).ready(function(){
    $("form").validate({
        rules: {
            '<?=CHtml::activeName($form,'name')?>': {
                required: true,
                rangelength: [2,12],
                exclude: ['@', ' '],
                remote: {
                    url: "<?=CHtml::normalizeUrl(array('/user/checkName'));?>",
                    type: "post",
                    data: {
                        name: function() {
                            return $("#<?=CHtml::activeId($form,'name')?>").val();
                        }
                    }
                }
            },
            '<?=CHtml::activeName($form,'password')?>': {
                required: true,
                rangelength: [6,20]
            },
            '<?=CHtml::activeName($form,'passwordRepeat')?>': {
                required: true,
                rangelength: [6,20],
                equalTo: "#<?=CHtml::activeId($form,'password')?>"
            },
            '<?=CHtml::activeName($form,'verifyCode')?>': {
                required: true,
                rangelength: [4,4]
            }
        },
        messages: {
            '<?=CHtml::activeName($form,'name')?>': {
                required: "用户名不能为空",
                rangelength: jQuery.format("用户名长度为{0}到{1}个字"),
                exclude: "用户名不能包含@或空格",
                remote: "用户名已经存在",
            },
            '<?=CHtml::activeName($form,'password')?>': {
                required: "密码不能为空",
                rangelength: jQuery.format("密码长度为{0}到{1}个字"),
            },
            '<?=CHtml::activeName($form,'passwordRepeat')?>': {
                required: "密码确认不能为空",
                rangelength: jQuery.format("密码确认长度为{0}到{1}个字"),
                equalTo: "两次密码输入不相同"
            },
            '<?=CHtml::activeName($form,'verifyCode')?>': {
                required: "验证码不能为空",
                rangelength: jQuery.format("验证码长度必须为{0}")
            }
        },
        errorPlacement: function(error, element) {
            error.appendTo(element.next());
        },
        success: function(label) {
            label.html("&nbsp;").addClass("checked");
        },
        onkeyup: false
    });
});

function refreshVerifyImage()
{
    str = '<?=CHtml::normalizeUrl(array('/verifyImage/show', 'name'=>'register'));?>?' + Math.random();
    $('#verify_image').attr('src', str);
    $('#verifyCode').attr('value', '');
}
</script>    

<div>
  <h2 class="title">注册</h2>

  <?php echo CHtml::beginForm('','post',array('id'=>'registerForm')); ?>
  <?php echo CHtml::errorSummary($form); ?>

  <div>
    <h3>
    <label id="l<?=CHtml::activeId($form,'name')?>" for="<?=CHtml::activeId($form,'name')?>"><?=CHtml::encode($form->getAttributeLabel('name'))?></label>
    </h3>
    <?php echo CHtml::activeTextField($form,'name',array('maxlength'=>12,'size'=>20)); ?>
    <span id='status'></span>
    <br />
    2-12个字，允许中文名，不能包含字符@和空格。
  </div>
  
  <div> 
    <h3>
    <label id="l<?=CHtml::activeId($form,'password')?>" for="<?=CHtml::activeId($form,'password')?>"><?=CHtml::encode($form->getAttributeLabel('password'))?></label>
    </h3>
    <?php echo CHtml::activePasswordField($form,'password',array('size'=>20)); ?>
    <span id='status'></span>
    <br />
    6-20个字符
  </div>

  <div> 
    <h3>
    <label id="l<?=CHtml::activeId($form,'passwordRepeat')?>" for="<?=CHtml::activeId($form,'passwordRepeat')?>"><?=CHtml::encode($form->getAttributeLabel('passwordRepeat'))?></label>
    </h3>
    <?php echo CHtml::activePasswordField($form,'passwordRepeat',array('size'=>20)); ?>
    <span id='status'></span>
    <br />
  </div>

  <div> 
    <h3>
    <label id="l<?=CHtml::activeId($form,'verifyCode')?>" for="<?=CHtml::activeId($form,'verifyCode')?>"><?=CHtml::encode($form->getAttributeLabel('verifyCode'))?></label>
    </h3>
    <?php echo CHtml::activeTextField($form,'verifyCode',array('size'=>VerifyImage::CODE_NUM, 'maxlength'=>VerifyImage::CODE_NUM, 'id'=>'verifyCode')); ?>
    <span id='status'></span>
    <img id="verify_image">
    <a href="javascript:refreshVerifyImage();">看不清，换一张</a>
  </div>  

  <br />

  <div class="center">
    <?php echo CHtml::submitButton('注册'); ?>
  </div>      
  
  <?php echo CHtml::endForm(); ?>
</div>
